package com.pangu.ui.browser;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ListView;

import com.pangu.AMApplication;
import com.pangu.task.mark.ATaskMark;
import com.pangu.ui.adapter.AGroupItemAdapter;

/**
 * 分组显示列表
 * 
 * @author cxlin
 */
public abstract class GroupItemBroswer<A extends AMApplication> extends LoadableList<A> implements
		AbsListView.OnScrollListener {

	// 滚动中
	protected boolean scrolling = true;

	public GroupItemBroswer(Context context) {
		super(context);
	}

	public GroupItemBroswer(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	public void initLoadableView(ATaskMark taskWraper, boolean autoLoad, boolean loadInit, boolean forceLoad) {
		if (mTaskMark != taskWraper) {
			scrolling = true;
		}
		super.initLoadableView(taskWraper, autoLoad, loadInit, forceLoad);
	}

	@Override
	protected void initView(Context context) {
		super.initView(context);

		((AbsListView) adapterView).setOnScrollListener(this);
	}

	@Override
	public void onViewRemove() {
		super.onViewRemove();

		scrolling = false;
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		switch (scrollState) {
		case AbsListView.OnScrollListener.SCROLL_STATE_IDLE:
			if (scrolling) {
				scrolling = false;
			}
			break;

		case AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL:
		case AbsListView.OnScrollListener.SCROLL_STATE_FLING:
			if (!scrolling) {
				scrolling = true;
			}
			break;
		}
	}

	/**
	 * 调用这个方法通知数据改变 初始化组菜单默认是展开的
	 */
	@Override
	public void notifyDataSetChanged() {
		// 由于在刷新时需要重置列表数据，在这重新实现基类的数据刷新
		// 初始化组数据
		ExpandableListView eListView = (ExpandableListView) adapterView;
		if (eListView == null) {
			return;
		}

		ExpandableListAdapter adapter = eListView.getExpandableListAdapter();
		if (adapter == null) {
			return;
		}

		AGroupItemAdapter gAdapter = null;
		if (adapter instanceof AGroupItemAdapter) {
			gAdapter = (AGroupItemAdapter) adapter;
		}

		int count = 0;
		if (gAdapter != null) {
			gAdapter.initItemGroup();
			gAdapter.notifyDataSetChanged();
			count = gAdapter.getGroupCount();

		} else {
			// 它至少是实现了ExpandableListAdapter的BaseAdapter对象
			((BaseAdapter) adapter).notifyDataSetChanged();
			count = adapter.getGroupCount();
		}

		// 初始化组菜单默认是展开的
		for (int index = 0, num = count; index < num; index++) {
			if (!eListView.isGroupExpanded(index)) {
				eListView.expandGroup(index);

			}
		}

		// 刷新空视图
		updateEmptyView();
	}

	@Override
	protected AdapterView createAdapterView() {
		ExpandableListView eListView = new ExpandableListView(getContext());

		setListViewParameter(eListView);
		// 添加头部
		addListHeader(eListView);
		addListFooter(eListView);

		return eListView;
	}

	/**
	 * 设置默认的ListView的风格和参数，子类不要将其重新实现
	 */
	protected void setListViewParameter(ExpandableListView eListView) {
		// eListView.setBackgroundResource(R.drawable.mui__common_view_bg);
		eListView.setGroupIndicator(null);
		eListView.setCacheColorHint(Color.TRANSPARENT);
		// Drawable drawable =
		// getResources().getDrawable(R.drawable.mui__list_divider_bg);
		// eListView.setDivider(drawable);
		// eListView.setChildDivider(drawable);

		// // 将列表的selector设置为透明，状态由子项自己设置
		// eListView.setSelector(new
		// ColorDrawable(getResources().getColor(R.color.mui__transparency)));
		eListView.setFadingEdgeLength(0);
	}

	/**
	 * 添加List的头部标题
	 * 
	 * @param listView
	 */
	protected void addListHeader(ListView listView) {
		// 空实现，子类要头部自己复盖。
	}

	/**
	 * @param listView
	 */
	protected void addListFooter(ListView listView) {
		// 空实现，子类要底部自己复盖。
	}

	/**
	 * 移除列表头部
	 * 
	 * @param header
	 */
	public void removeListHeader(View header) {
		((ListView) adapterView).removeHeaderView(header);
	}

	/**
	 * 移除列表底部
	 * 
	 * @param footer
	 */
	public void removeListFooter(View footer) {
		((ListView) adapterView).removeFooterView(footer);
	}

}
